package com.bawei.detail.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.common.mvi.BaseViewModel
import com.bawei.detail.DetailUiState
import com.bawei.detail.DetailViewIntent
import com.bawei.detail.repos.DetailRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class DetailViewModel @Inject constructor(private val repository: DetailRepository): BaseViewModel<DetailViewIntent>() {

    private val _uiState = MutableStateFlow<DetailUiState>(DetailUiState.LoadEmpty)
    val uiState: StateFlow<DetailUiState> = _uiState

    override fun handIntent(uiIntent: DetailViewIntent) {
        when(uiIntent){
            is DetailViewIntent.LoadAuthorInfo -> loadAuthorInfo(uiIntent.userId)
            is DetailViewIntent.LoadRecommendMovieList -> loadRecommendMovie(uiIntent.page, uiIntent.pageSize)
            is DetailViewIntent.LoadMovieList -> loadMovieList(uiIntent.typeId, uiIntent.page, uiIntent.pageSize)
            is DetailViewIntent.LoadMovieCommends -> loadMovieCommends(uiIntent.dataType, uiIntent.itemId)
        }

    }

    private fun loadMovieCommends(dataType: Int, itemId: String) {
        viewModelScope.launch {
            try {
                val result = repository.getVideoCommends(dataType, itemId)
                if(result.code == 0){
                    _uiState.value = DetailUiState.LoadMovieCommendSuccess(result.data)
                }else{
                    _uiState.value = DetailUiState.LoadMovieCommendFail(result.msg)
                }
            } catch (e: Exception){
                _uiState.value = DetailUiState.LoadMovieCommendFail("数据加载失败！")
            }
        }
    }


    private fun loadAuthorInfo(userId: String){
        viewModelScope.launch {
            try {
                val result = repository.getAuthorById(userId)
                if(result.code == 0){
                    _uiState.value = DetailUiState.LoadAuthorSuccess(result.data)
                }else{
                    _uiState.value = DetailUiState.LoadAuthorFail(result.msg)
                }
            } catch (e: Exception){
                _uiState.value = DetailUiState.LoadAuthorFail("数据加载失败！")
            }
        }
    }

    private fun loadMovieList(typeId: Int, page: Int, pageSize: Int) {
        viewModelScope.launch {
            try {
                val result = repository.getMultiVideoByTypeId(typeId, page, pageSize)
                if(result.code == 0){
                    _uiState.value = DetailUiState.LoadMovieListSuccess(result.data)
                }else{
                    _uiState.value = DetailUiState.LoadMovieListFail(result.msg)
                }
            } catch (e: Exception){
                _uiState.value = DetailUiState.LoadMovieListFail("数据加载失败！")
            }
        }

    }

    private fun loadRecommendMovie(page: Int, pageSize: Int) {
        viewModelScope.launch {
            try {
                val result = repository.getRecommendMultiVideo(page, pageSize)
                if(result.code == 0){
                    _uiState.value = DetailUiState.LoadMovieListSuccess(result.data)
                }else{
                    _uiState.value = DetailUiState.LoadMovieListFail(result.msg)
                }
            } catch (e: Exception){
                _uiState.value = DetailUiState.LoadMovieListFail("数据加载失败！")
            }
        }
    }





}